package sedev.shopping.util;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Connector {
	private static Connector _instance;
    private Connection _conn;
    private ResultSet _result_set;
    private PreparedStatement _pre_statement;

    private Connector(){        
    }

    public static Connector getInstance(){
        if(_instance==null){
            _instance=new Connector();
        }
        return _instance;
    }

    @SuppressWarnings("deprecation")
	private void openConnection(){
        try{
            /*if(_conn==null)
            {
    			_conn = HibernateUtil.getSession().openSession().connection();
            }*/        	
            _conn = HibernateUtil.getSession().openSession().connection();
            if(this._pre_statement!=null){
            	this._pre_statement.close();
            }
            if(this._result_set!=null){
            	this._result_set.close();
            	
            }
        }catch(Exception ex){
            _conn=null;
        }
    }

    private void closeConnection(){
        try{
            if(_conn != null)
                _conn.close();
            if(this._pre_statement != null)
            	this._pre_statement.close();
            if(this._result_set != null){            	
            	this._result_set.close();
            }
        }catch(Exception e){
            
        }
    }
    
    @Override
    protected void finalize(){
        try {
            super.finalize();
        } catch (Throwable ex) {
            Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.closeConnection();
    }

    public boolean executeNoneQuery(String query){
        int row = 0;
        try {
            this.openConnection();
            this._pre_statement = this._conn.prepareStatement(query);
            row = this._pre_statement.executeUpdate();
            if(row != 0)
                return true;
        } catch (SQLException ex) {
            return false;
        }
        return false;
    }

    public ResultSet executeQuery(String query){
        try {
            this.openConnection();
            this._pre_statement = this._conn.prepareStatement(query);
            this._result_set = this._pre_statement.executeQuery();
            return this._result_set;            
        } catch (SQLException ex) {
            return null;
        }
    }

    public Long getTotalRecord(String tablename,String condition){
        Long totalrecord= new Long(0);
        StringBuilder query=new StringBuilder();
        query.append("select count(*) from ").append(tablename);
        query.append(" where ").append(condition);
        ResultSet result=this.executeQuery(query.toString());
        try{
            if(result!=null && result.next()){
                totalrecord=result.getLong(1);
            }
        }catch(SQLException sqle){
            return totalrecord;
        }
        return totalrecord;
    }
    
    public Long getNextId(){
        StringBuilder query=new StringBuilder();
        query.append("select LAST_INSERT_ID()");
        ResultSet result=this.executeQuery(query.toString());
        try{
            if(result!=null && result.next()){
                return result.getLong(1);
            }
        }catch(SQLException sqle){
            return null;
        }
        return null;
    }
}
